package com.mt.sdk.longdatas.protocol;

import com.mt.sdk.ble.model.MTBeaconInfModel;
import com.mt.sdk.tools.MTTools;

public class MTBeacon4Protocol {

	private TransferprotocolHelp transprotocolhelp;

	public MTBeacon4Protocol() {
		transprotocolhelp = new TransferprotocolHelp();
		transprotocolhelp.setCallback(transcallback);
	}

	// 读取广播数据
	public static byte[] getPrivateBeaconParams(int id) {
		byte[] datas = new byte[3];
		int lenght = 0;
		datas[lenght++] = 0x0E; // 命令

		datas[lenght++] = (byte) ((id >> 0) & 0xFF); // ID

		datas[datas.length - 1] = TransferprotocolHelp.getLRCCheckByte(datas, // 校验
				datas.length - 1); // 添加校验
		datas = TransferprotocolHelp.packgeDatas(datas);
		return datas;
	}

	// 0X4C, 0X00, 苹果ID
	// 0X02, 0X15, IBeaconID
	// 0XFD, 0XA5, 0X06, 0X93, UUID
	// 0XA4, 0XE2, \
	// 0X4F, 0XB1, \
	// 0XAF, 0XCF, \
	// 0XC6, 0XEB, 0X07, 0X64, 0X78, 0X25,\
	// 0X27, 0X12,\ major
	// 0X21, 0X1F,\ minor
	// 0XC0\ measurepower
	// 设置广播数据
	public static byte[] setPrivateBeaconParams(MTBeaconInfModel mPrivateBeaconParams) {
		byte[] datas = new byte[29];
		int lenght = 0;
		datas[lenght++] = 0x0F; // 命令

		datas[lenght++] = (byte) ((mPrivateBeaconParams.getBeaconid() >> 0) & 0xFF); // ID号
		datas[lenght++] = (byte) ((mPrivateBeaconParams.getWeight() >> 0) & 0xFF);// 广播频率

		datas[lenght++] = 0x4C; // 苹果ID
		datas[lenght++] = 0x00;

		datas[lenght++] = 0x02; // IBeacon ID
		datas[lenght++] = 0x15;

		byte[] uuid = MTTools.convertStringToByteArray(mPrivateBeaconParams.getBeaconUUID()); // UUID
		MTTools.arraycopy(uuid, 0, datas, lenght, uuid.length);
		lenght += uuid.length;

		datas[lenght++] = MTTools.converHiUint16(mPrivateBeaconParams.getMajor()); // major
		datas[lenght++] = MTTools.converLoUint16(mPrivateBeaconParams.getMajor());

		datas[lenght++] = MTTools.converHiUint16(mPrivateBeaconParams.getMinor()); // major
		datas[lenght++] = MTTools.converLoUint16(mPrivateBeaconParams.getMinor());

		datas[lenght++] = (byte) (-Math.abs(mPrivateBeaconParams.getMeasuedPower())); // txpower

		datas[datas.length - 1] = TransferprotocolHelp.getLRCCheckByte(datas, // 校验
				datas.length - 1); // 添加校验
		datas = TransferprotocolHelp.packgeDatas(datas);

		System.out.println("发送数据->" + MTTools.convertBytearrayToString(datas));
		return datas;
	}

	// 读取Beacon公共参数
	public static byte[] getPublicBeaconParams() {
		byte[] datas = new byte[2];
		int lenght = 0;
		datas[lenght++] = 0x09; // 命令

		datas[datas.length - 1] = TransferprotocolHelp.getLRCCheckByte(datas, // 校验
				datas.length - 1); // 添加校验
		datas = TransferprotocolHelp.packgeDatas(datas);
		return datas;
	}

	// 设置Beacon公共参数
	public static byte[] setPublicBeaconParams(MTBeaconInfModel mPublicBeaconParams) {
		byte[] datas = new byte[4];
		int lenght = 0;
		datas[lenght++] = 0x0A; // 命令

		datas[lenght++] = MTTools.converLoUint16(mPublicBeaconParams.getSendhz() / 100);

		datas[lenght++] = MTTools.converLoUint16(mPublicBeaconParams.getSendpower()); // 发射功率

		datas[datas.length - 1] = TransferprotocolHelp.getLRCCheckByte(datas, datas.length - 1); // 添加校验

		datas = TransferprotocolHelp.packgeDatas(datas);

		System.out.println("setPublicBeaconParams->" + MTTools.convertBytearrayToString(datas));
		return datas;
	}

	// 部署
	public static byte[] setDeverlop(int id, String msg) {
		byte[] datas;
		int lenght = 0;

		if (id == 66) {
			if (msg == null) {
				return null;
			}
			byte[] msg_byte = msg.getBytes();
			datas = new byte[msg_byte.length + 3];

			datas[lenght++] = 0x10; // 命令
			datas[lenght++] = (byte) ((id >> 0) & 0xFF); // 部署方式
			MTTools.arraycopy(msg_byte, 0, datas, lenght, msg_byte.length);
		} else {
			datas = new byte[3];
			datas[lenght++] = 0x10; // 命令
			datas[lenght++] = (byte) ((id >> 0) & 0xFF); // 部署方式
		}

		datas[datas.length - 1] = TransferprotocolHelp.getLRCCheckByte(datas, datas.length - 1); // 添加校验

		datas = TransferprotocolHelp.packgeDatas(datas);
		return datas;
	}

	// 密码验证
	public static byte[] verifyPwd(String pwd) {

		if (pwd == null) {
			return null;
		}

		byte[] pwd_byte = pwd.getBytes();

		byte[] datas = new byte[pwd_byte.length + 2];
		int lenght = 0;
		datas[lenght++] = 0x0D; // 命令

		MTTools.arraycopy(pwd_byte, 0, datas, lenght, pwd_byte.length); // 密码

		datas[datas.length - 1] = TransferprotocolHelp.getLRCCheckByte(datas, datas.length - 1); // 添加校验

		datas = TransferprotocolHelp.packgeDatas(datas);
		return datas;
	}

	// 读取固件版本号
	public static byte[] getVersion() {
		byte[] datas = new byte[2];
		int lenght = 0;

		datas[lenght++] = 0x06;

		datas[datas.length - 1] = TransferprotocolHelp.getLRCCheckByte(datas, datas.length - 1); // 添加校验

		datas = TransferprotocolHelp.packgeDatas(datas);
		return datas;
	}

	// 接收短数据进入协议进行拆包
	public void reviceDatas(byte[] datas) {
		transprotocolhelp.putDatas(datas);
	}

	// 接收数据回调
	private TransferprotocolHelp.TransferprotocolHelpCallBack transcallback = new TransferprotocolHelp.TransferprotocolHelpCallBack() {

		@Override
		public void onGetcmd(byte[] cmd) {
			if (!TransferprotocolHelp.checkErro(cmd)) { // 校验错误
				System.out.println("校验错误");
				return;
			}
			System.out.println("解包数据->" + MTTools.convertBytearrayToString(cmd));
			analysisReviceCmd(cmd);
		}
	};

	// 回调
	public static interface MTBeacon4ProtocolCallBack {
		public void unKnowDatas(byte[] cmd);

		public void onGetPrivateBeaconParams(MTBeaconInfModel params, boolean statues, int reason);

		public void onSetPrivateBeaconParams(boolean statues, int reason);

		public void onGetPublicBeaconParams(MTBeaconInfModel params, boolean statues, int reason);

		public void onSetPublicBeaconParams(boolean statues, int reason);

		public void onSetDeverlop(boolean statues, int reason);

		public void onVerifyPwd(boolean statues, int reason);

		public void onGetVersion(String version, boolean statues, int reason);
	}

	// 设置回调数据
	private MTBeacon4ProtocolCallBack mProtocolCallBack;

	public void setCallBack(MTBeacon4ProtocolCallBack mProtocolCallBack) {
		this.mProtocolCallBack = mProtocolCallBack;
	}

	// 获取错误解析
	private static final String[] ERROMSG = { "指令校验失败", "指令ID错误", "指令长度错误", "提醒器内存不足", "指令参数超出范围", "指令参数错误", "需要验证密码",
			"密码验证失败", "文件名错误", "文件过大" };

	// 分析数据
	private void analysisReviceCmd(byte[] cmd) {
		if (mProtocolCallBack == null) {
			return;
		}

		boolean erro_flag = false;
		if (cmd[0] < 0) {
			if (cmd.length < 4) {
				return;
			}
			erro_flag = true;
			cmd[0] = (byte) (cmd[0] & 0x0F);
			if ((cmd[2] < 1) || (cmd[2] > ERROMSG.length)) {
				return;
			}
		}

		if (cmd[0] == 0x0E) { // 读取广播数据
			
			if (erro_flag) {
				mProtocolCallBack.onGetPrivateBeaconParams(null, false, 0);
				return;
			}
			
			if (cmd.length != 29) {
				MTBeaconInfModel mPrivateBeaconParams = new MTBeaconInfModel();
				mPrivateBeaconParams.setBeaconid(MTTools.convertUint8(cmd[1]));
				mProtocolCallBack.onGetPrivateBeaconParams(mPrivateBeaconParams, true, 0);
				return;
			}

			MTBeaconInfModel mPrivateBeaconParams = new MTBeaconInfModel();
			mPrivateBeaconParams.setBeaconid(MTTools.convertUint8(cmd[1]));
			mPrivateBeaconParams.setWeight(MTTools.convertUint8(cmd[2]));
			String uuid_str = MTTools.convertByteToString(cmd[7]) + MTTools.convertByteToString(cmd[8])
					+ MTTools.convertByteToString(cmd[9]) + MTTools.convertByteToString(cmd[10]) + "-"
					+ MTTools.convertByteToString(cmd[11]) + MTTools.convertByteToString(cmd[12]) + "-"
					+ MTTools.convertByteToString(cmd[13]) + MTTools.convertByteToString(cmd[14]) + "-"
					+ MTTools.convertByteToString(cmd[15]) + MTTools.convertByteToString(cmd[16]) + "-"
					+ MTTools.convertByteToString(cmd[17]) + MTTools.convertByteToString(cmd[18])
					+ MTTools.convertByteToString(cmd[19]) + MTTools.convertByteToString(cmd[20])
					+ MTTools.convertByteToString(cmd[21]) + MTTools.convertByteToString(cmd[22]);
			mPrivateBeaconParams.setBeaconUUID(uuid_str);
			mPrivateBeaconParams.setMajor(MTTools.convertUint16(cmd[23], cmd[24]));
			mPrivateBeaconParams.setMinor(MTTools.convertUint16(cmd[25], cmd[26]));
			mPrivateBeaconParams.setMeasuedPower(Math.abs(cmd[27]));
			mProtocolCallBack.onGetPrivateBeaconParams(mPrivateBeaconParams, true, 0);
			return;
		}

		if (cmd[0] == 0x0F) { // 设置广播参数
			
			if (erro_flag) {
				mProtocolCallBack.onSetPrivateBeaconParams(false, 0);
				return;
			}
			
			mProtocolCallBack.onSetPrivateBeaconParams(true, 0);
			return;
		}

		if (cmd[0] == 0x09) { // 读取Beacon公共参数
			if (erro_flag || (cmd.length != 4)) {
				mProtocolCallBack.onGetPublicBeaconParams(null, false, 0);
				return;
			}

			MTBeaconInfModel mPublicBeaconParams = new MTBeaconInfModel();
			mPublicBeaconParams.setSendhz(MTTools.convertUint8(cmd[1]) * 100);
			mPublicBeaconParams.setSendpower(MTTools.convertUint8(cmd[2]));
			mProtocolCallBack.onGetPublicBeaconParams(mPublicBeaconParams, true, 0);
			return;
		}

		if (cmd[0] == 0x0A) { // 设置Beacon公共参数
			if (erro_flag) {
				mProtocolCallBack.onSetPublicBeaconParams(false, 0);
				return;
			}
			
			mProtocolCallBack.onSetPublicBeaconParams(true, 0);
			return;
		}

		if (cmd[0] == 0x10) { // 设置部署模式
			if (erro_flag) {
				mProtocolCallBack.onSetDeverlop(false, 0);
				return;
			}
			mProtocolCallBack.onSetDeverlop(true, 0);
			return;
		}

		if (cmd[0] == 0x0D) { // 密码验证
			if (erro_flag) {
				mProtocolCallBack.onVerifyPwd(false, 0);
				return;
			}
			
			mProtocolCallBack.onVerifyPwd(true, 0);
			return;
		}

		if (cmd[0] == 0x06) { // 读取固件版本号
			if (erro_flag || (cmd.length < 2)) {
				mProtocolCallBack.onGetVersion(null, false, 0);
				return;
			}
			byte[] version_byte = new byte[cmd.length - 2];

			MTTools.arraycopy(cmd, 1, version_byte, 0, version_byte.length);

			String version_str = new String(version_byte);

			mProtocolCallBack.onGetVersion(version_str, true, 0);

			return;
		}
	}
}
